package com.qiaoyanqing.ximalaya.discovermodule.discover_adapter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.AnchorDetailsActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_AnchorStytle;
import com.qiaoyanqing.ximalaya.minemodule.mine_fragment.LoginActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 27265 on 2016/12/2.
 */

public class Discover_Anchor_Adapter extends BaseAdapter {
    private String title;
    private Context context;
    private List<Discover_AnchorStytle> mDiscoverAnchorStytles = new ArrayList<>();
    private TextView anchorauthor;
    private TextView author1;
    private TextView author2;
    private TextView author3;
    private ImageView image1;
    private ImageView image2;
    private ImageView image3;

    public Discover_Anchor_Adapter(Context context, List<Discover_AnchorStytle> discoverAnchorStytles) {
        this.context = context;
        this.mDiscoverAnchorStytles = discoverAnchorStytles;

        }

    @Override
    public int getCount() {
        //  MyLog.i("falg","------mDiscoverAnchorStytles"+mDiscoverAnchorStytles.size());
        return mDiscoverAnchorStytles == null ? 0 : mDiscoverAnchorStytles.size();
    }

    @Override
    public Object getItem(int position) {
        return mDiscoverAnchorStytles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.anchor_item_layout, parent, false);
            holder.anchorauthor = ((TextView) convertView.findViewById(R.id.anchorauthor));
            holder.author1 = ((TextView) convertView.findViewById(R.id.author1));
            holder.author2 = ((TextView) convertView.findViewById(R.id.author2));
            holder.author3 = ((TextView) convertView.findViewById(R.id.author3));
            holder.image1 = ((ImageView) convertView.findViewById(R.id.image1));
            holder.image2 = ((ImageView) convertView.findViewById(R.id.image2));
            holder.image3 = ((ImageView) convertView.findViewById(R.id.image3));
            holder.more = ((Button) convertView.findViewById(R.id.more));
            holder.btn1 = ((Button) convertView.findViewById(R.id.btn1));
            holder.btn2 = ((Button) convertView.findViewById(R.id.btn2));
            holder.btn3 = ((Button) convertView.findViewById(R.id.btn3));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.anchorauthor.setText(mDiscoverAnchorStytles.get(position).getTitle());
        holder.author1.setText(mDiscoverAnchorStytles.get(position).getList().get(0).getNickname());
        holder.author2.setText(mDiscoverAnchorStytles.get(position).getList().get(1).getNickname());
        holder.author3.setText(mDiscoverAnchorStytles.get(position).getList().get(2).getNickname());
        String imageUrl1 = mDiscoverAnchorStytles.get(position).getList().get(0).getSmallLogo();
        String imageUrl2 = mDiscoverAnchorStytles.get(position).getList().get(1).getSmallLogo();
        String imageUrl3 = mDiscoverAnchorStytles.get(position).getList().get(2).getSmallLogo();

        String icpath = "http://s1.sns.maimaicha.com/images/2016/01/06/20160106110314_22333_suolue3.jpg";

        Picasso.with(context).load(imageUrl1.length() != 0 ? imageUrl1 : icpath).into(holder.image1);
        Picasso.with(context).load(imageUrl2.length() != 0 ? imageUrl2 : icpath).into(holder.image2);
        Picasso.with(context).load(imageUrl3.length() != 0 ? imageUrl3 : icpath).into(holder.image3);

        holder.image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context, "这是第一张图片", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("uid",mDiscoverAnchorStytles.get(position).getList().get(0).getUid());
                intent.putExtra("title",mDiscoverAnchorStytles.get(position).getTitle());
                intent.setClass(context, AnchorDetailsActivity.class);
                context.startActivity(intent);
            }
        });
        holder.image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context, "这是第二张图片", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("uid",mDiscoverAnchorStytles.get(position).getList().get(1).getUid());
                intent.putExtra("title",mDiscoverAnchorStytles.get(position).getTitle());
                intent.setClass(context, AnchorDetailsActivity.class);
                context.startActivity(intent);
            }
        });

        holder.image3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context, "这是第三张图片", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("uid",mDiscoverAnchorStytles.get(position).getList().get(2).getUid());
                intent.putExtra("title",mDiscoverAnchorStytles.get(position).getTitle());
                intent.setClass(context, AnchorDetailsActivity.class);
                context.startActivity(intent);
            }
        });

        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        holder.btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        holder.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        holder.btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        private TextView anchorauthor, author1, author2, author3;
        private ImageView image1, image2, image3;
        private Button more, btn1, btn2, btn3;
    }


}
